<template>
  <el-dialog :title="title" :visible="visible" @close="handleClose" width="800px">
    <el-form :model="formData" label-position="right" label-width="90px" ref="formRef" :rules="rules">
      <el-form-item label="规则" prop="selectValue">
        <el-select clearable multiple v-model="formData.selectValue" filterable style="width: 100%;">
          <el-option v-for="(item,index) in ruleArea" :key="index" :value="item.id" :label="item.ruleName"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="仓库" prop="stoId">
        <el-select multiple style="width: 100%;" clearable filterable v-model="formData.stoId">
          <el-option v-for="(item,index) in wareList" :key="index" :value="item.stoId" :label="item.stoName"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="saveForm">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import {
  getAllWarehouseList,//仓库下拉
} from "@/api/index/yxl.js"
  import {
    associationRule //提交规则关联
  } from '@/api/basic/PersonnelInformation.js'
  export default {
    data() {
      return {
        wareList: [],
        title: '关联仓库',
        multipleSelection: [], //多选的table列表
        ruleArea: [],
        formData: {
          stoId: '',
          selectValue: [],
        },
        dataForm:{},
        rules: {
          selectValue: [
            { required: true, message: '请选择规则', trigger: 'blur' },
          ],
          stoId: [
            { required: true, message: '请选择仓库', trigger: 'blur' },
          ],
        }
      }
    },
    props: {
      visible: { // 接收父组件通过sync传递的visible属性
        type: Boolean,
        default: false
      }
    },
    created() {
      this.getList();
    },
    methods: {
      //多选
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      openDialog(row) {
        this.ruleArea = row.children;
        this.dataForm = row;
      },
      // 提取重置表单的逻辑
      handleClose() {
        // 通过事件通知父组件更新visible值，触发update:visible事件
        this.$nextTick(() => {
          this.$refs.formRef.resetFields();
        })
        this.$emit('update:visible', false);
      },
      //提交规则关联
      saveForm() {
        this.$refs.formRef.validate((valid) => {
          if (valid) {
            let param = {
              type:this.dataForm.id,
            }
            let data = {
              ruleId:this.formData.selectValue,
              association:this.formData.stoId
            }
            associationRule(param,data).then(res=>{
              this.$emit('addSuccess'); // 通知父组件添加成功
              this.$message({
                message: res.msg, // 显示成功消息
                type: 'success',
              });
              this.handleClose();
            })
          }
        })
      },
      //获取仓库信息
      getList() {
        getAllWarehouseList().then((response) => {
          this.wareList = response.data;
        });
      }
    }
  }
</script>

<style>
</style>
